package com.entities.user.servicelayer;

import com.core.hibernate.HibernateFetchRequest;
import com.core.protocols.IHibernateParentEnumeratorDelegate;
import com.core.protocols.IKVObject;
import com.core.protocols.IServiceLayerDelegate;
import com.core.servicelayer.HibernateServiceLayer;
import com.core.utils.StringUtils;
import com.model.KeyValue;
import com.model.user.User;

public class UserListLayer
        extends HibernateServiceLayer {

	public Class <? extends IKVObject> entityClass () {

		return User.class;
	}



	public void enumerateParentObjectsWithDelegate ( IKVObject kvObject,
	                                                 IHibernateParentEnumeratorDelegate delegate ) {

	}



	public void resetPassword ( Integer userId,
	                            String value,
	                            IServiceLayerDelegate delegate ) {

		KeyValue kv = new KeyValue("applyPasswordMD5String", value);

		this.setCustomProperty(userId, kv, delegate);

	}



	public void checkValidPassword ( String userName,
	                                 String userPass ) {

		if ( userName == null ) {
			return;
		}
		String userMD5Pass = null;
		if ( userPass != null ) {
			userMD5Pass = String.format("'%s'", StringUtils.md5FromString(userPass));
		}


		HibernateFetchRequest request = new HibernateFetchRequest();
		request.setDelegate(this);
		String predicate = String.format("where USER.name = '%s' and USER.passwordHashString = %s", userName, userMD5Pass);

		// Set <HibernateParameter> params = new HashSet <HibernateParameter>();
		// params.add(new HibernateParameter("USER", userName));
		// params.add(new HibernateParameter("HASH", userMD5Pass));
		request.performFetch(User.class, predicate, null);

	}



}
